package com.hanserwei.han_picture.controller;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hanserwei.han_picture.constant.SpaceUserPermissionConstant;
import com.hanserwei.han_picture.domain.common.ApiResponse;
import com.hanserwei.han_picture.domain.common.DeleteRequest;
import com.hanserwei.han_picture.domain.entity.po.SpaceUser;
import com.hanserwei.han_picture.domain.entity.po.User;
import com.hanserwei.han_picture.domain.entity.vo.SpaceUserVO;
import com.hanserwei.han_picture.domain.exception.ApiException;
import com.hanserwei.han_picture.domain.request.spaceuser.SpaceUserAddRequest;
import com.hanserwei.han_picture.domain.request.spaceuser.SpaceUserEditRequest;
import com.hanserwei.han_picture.domain.request.spaceuser.SpaceUserQueryRequest;
import com.hanserwei.han_picture.enums.ResponseCodeEnum;
import com.hanserwei.han_picture.manager.auth.annotation.SaSpaceCheckPermission;
import com.hanserwei.han_picture.service.SpaceUserService;
import com.hanserwei.han_picture.service.UserService;
import com.hanserwei.han_picture.utils.ThrowUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 空间用户关联表(space_user)表控制层
 *
 * @author hanserwei
 */
@RestController
@RequestMapping("/spaceUser")
@Tag(name = "SpaceUserController")
@RequiredArgsConstructor
public class SpaceUserController {
    private final SpaceUserService spaceUserService;
    private final UserService userService;

    @PostMapping("/add")
    @Operation(summary = "添加空间用户")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public ApiResponse<Boolean> addSpaceUser(@RequestBody SpaceUserAddRequest request) {
        ThrowUtils.throwIf(request == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        boolean added = spaceUserService.addSpaceUser(request);
        ThrowUtils.throwIf(!added, ResponseCodeEnum.SYSTEM_ERROR, "添加空间成员失败！");
        return ApiResponse.success("添加成功！", true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除空间用户,仅拥有成员管理权限的用户")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public ApiResponse<Boolean> deleteSpaceUser(@RequestBody DeleteRequest request) {
        ThrowUtils.throwIf(request == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        SpaceUser oldSpaceUser = spaceUserService.getById(request.getId());
        ThrowUtils.throwIf(oldSpaceUser == null, ResponseCodeEnum.INVALID_PARAMETER, "空间用户不存在！");
        // 删除空间用户
        boolean deleted = spaceUserService.removeById(request.getId());
        ThrowUtils.throwIf(!deleted, ResponseCodeEnum.SYSTEM_ERROR, "删除空间用户失败！");
        return ApiResponse.success("删除成功！", true);
    }

    @Operation(summary = "查询成员在指定空间的信息，仅拥有成员管理权限的用户")
    @PostMapping("/get")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public ApiResponse<SpaceUser> getSpaceUser(@RequestBody SpaceUserQueryRequest request) {
        ThrowUtils.throwIf(request == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        Long userId = request.getUserId();
        Long spaceId = request.getSpaceId();
        if (ObjUtil.hasNull(userId, spaceId)) {
            throw new ApiException(ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        }
        // 查询空间用户
        QueryWrapper<SpaceUser> queryWrapper = spaceUserService.getQueryWrapper(request);
        SpaceUser spaceUser = spaceUserService.getOne(queryWrapper);
        ThrowUtils.throwIf(spaceUser == null, ResponseCodeEnum.INVALID_PARAMETER, "空间用户不存在！");
        return ApiResponse.success("查询成功！", spaceUser);
    }

    @Operation(summary = "查询团队空间内的所有成员，仅拥有成员管理权限的用户可使用。")
    @PostMapping("/list")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public ApiResponse<List<SpaceUserVO>> listSpaceUser(@RequestBody SpaceUserQueryRequest request) {
        ThrowUtils.throwIf(request == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        QueryWrapper<SpaceUser> queryWrapper = spaceUserService.getQueryWrapper(request);
        List<SpaceUser> spaceUserList = spaceUserService.list(queryWrapper);
        List<SpaceUserVO> spaceUserVOList = spaceUserService.getSpaceUserVOList(spaceUserList);
        return ApiResponse.success("查询成功！", spaceUserVOList);
    }

    @Operation(summary = "更新空间用户信息")
    @PostMapping("/edit")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public ApiResponse<Boolean> editSpaceUser(@RequestBody SpaceUserEditRequest request) {
        ThrowUtils.throwIf(request == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        SpaceUser spaceUser = new SpaceUser();
        BeanUtils.copyProperties(request, spaceUser);
        spaceUserService.validSpaceUser(spaceUser, false);
        // 判断是否存在
        Long spaceUserId = request.getId();
        SpaceUser oldSpaceUser = spaceUserService.getById(spaceUserId);
        ThrowUtils.throwIf(oldSpaceUser == null, ResponseCodeEnum.INVALID_PARAMETER, "空间用户不存在！");
        // 执行编辑
        boolean updated = spaceUserService.updateById(spaceUser);
        ThrowUtils.throwIf(!updated, ResponseCodeEnum.SYSTEM_ERROR, "更新空间用户失败！");
        return ApiResponse.success("更新成功！", true);
    }

    @Operation(summary = "查询成员加入的团队空间列表，所有已登录用户可使用")
    @PostMapping("/list/my")
    public ApiResponse<List<SpaceUserVO>> listMyTeamSpaces(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        SpaceUserQueryRequest spaceUserQueryRequest = new SpaceUserQueryRequest();
        spaceUserQueryRequest.setUserId(loginUser.getId());
        QueryWrapper<SpaceUser> queryWrapper = spaceUserService.getQueryWrapper(spaceUserQueryRequest);
        List<SpaceUser> spaceUserList = spaceUserService.list(queryWrapper);
        List<SpaceUserVO> spaceUserVOList = spaceUserService.getSpaceUserVOList(spaceUserList);
        return ApiResponse.success("查询成功！", spaceUserVOList);
    }
}
